//
//  CYVideoDetail_CommentTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/23.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYVideoDetail_CommentTableViewCell.h"

#import "CYVideoDetail_CommentReplyTableViewCell.h"
#import "CYCommentReplyListViewController.h"

@implementation CYVideoDetail_CommentTableViewCell

+ (instancetype)initWithTableView:(UITableView *)tableView Identifier:(NSString *)identifier{
    CYVideoDetail_CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYVideoDetail_CommentTableViewCell" owner:self options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.replyTableView registerNib:[UINib nibWithNibName:@"CYVideoDetail_CommentReplyTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYVideoDetail_CommentReplyTableViewCell"];

    self.headImageVIew.layer.cornerRadius = self.headImageVIew.frame.size.height / 2;
    self.headImageVIew.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoGeRenZhuYe)];
    [self.headImageVIew addGestureRecognizer:tap];
    self.headImageVIew.userInteractionEnabled = YES;
    
    self.replyTableView.scrollEnabled = NO;
}

- (void)gotoGeRenZhuYe{
    CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
    zhuye.userId = self.commentModel.userId;
    [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
}

- (IBAction)zanAction:(UIButton *)sender {
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    if (self.zanblock) {
        self.zanblock(self.commentModel.userId, self.commentModel.ID);
    }
}

- (IBAction)commentAction:(UIButton *)sender {
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    if (self.block) {
        self.block(self.commentModel.userId, self.commentModel.ID);
    }
}

- (void)setCommentModel:(CYCommentModel *)commentModel{
    _commentModel = commentModel;
    [self.headImageVIew sd_setImageWithURL:[NSURL URLWithString:commentModel.headImg] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageVIew.frame.size]];
    self.nicknameLabel.text = commentModel.username;
    self.commentContentLabel.text = commentModel.content;
    self.timeLabel.text = commentModel.timeStr;
    [self.zanBtn setTitle:[NSString stringWithFormat:@"  %@",commentModel.likeNumber] forState:UIControlStateNormal];
    [self.pinlunBtn setTitle:[NSString stringWithFormat:@"  %@",commentModel.replyNumber] forState:UIControlStateNormal];
    [self jisuanCellHeight];
 
    if ([commentModel.isLike integerValue] == 1) {
        self.zanBtn.selected = YES;
    }else{
        self.zanBtn.selected = NO;
    }
    
    
}

- (void)jisuanCellHeight{
    
    //计算评论内容高度
    CGFloat commentHeight = [[Tool sharedInstance] heightForString:self.commentModel.content andWidth:kWindowW - 74 fontSize:17];
    self.commentLabelHeightConstraint.constant = commentHeight;
    //计算回复高度
    CGFloat replyHeight = 0;
    NSArray *replies = self.commentModel.replies;
    if ([self.commentModel.replyNumber integerValue] > 2) {
        for (int i = 0; i < 2; i++) {
            CYCommentModel *model = replies[i];
            replyHeight += [[Tool sharedInstance] heightForString:[NSString stringWithFormat:@"%@:%@",model.username,model.content] andWidth:kWindowW - 104 fontSize:14];
            replyHeight += 15;
        }
        replyHeight += 40;
    }else{
        for (int i = 0; i < replies.count; i++) {
            CYCommentModel *model = replies[i];
            replyHeight += [[Tool sharedInstance] heightForString:[NSString stringWithFormat:@"%@:%@",model.username,model.content] andWidth:kWindowW - 104 fontSize:14];
            replyHeight += 15;
        }
    }
    self.replyTableViewHeightConstraint.constant = replyHeight;

    if (replies.count == 0) {
        self.cellHeight = commentHeight + replyHeight + 75;
        self.tableViewMarginTopConstraint.constant = 0;
    }else{
        self.cellHeight = commentHeight + replyHeight + 90;
        self.tableViewMarginTopConstraint.constant = 15;
    }
    [self.replyTableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.commentModel.replyNumber integerValue] > 2 ? 2 : self.commentModel.replies.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CYVideoDetail_CommentReplyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYVideoDetail_CommentReplyTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYVideoDetail_CommentReplyTableViewCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *replys = self.commentModel.replies;
    cell.replyModel = replys[indexPath.row];
    cell.block = ^(NSNumber * _Nonnull commentId, NSNumber * _Nonnull toUserId) {
        if (self.block) {
            self.block(toUserId, commentId);
        }
    };
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat replyHeight = 0;
    NSArray *replies = self.commentModel.replies;
    CYCommentModel *model = replies[indexPath.row];
    replyHeight += [[Tool sharedInstance] heightForString:[NSString stringWithFormat:@"%@:%@",model.username,model.content] andWidth:kWindowW - 104 fontSize:14];
    NSLog(@"replyHeight -- %f",replyHeight);
    return replyHeight + 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if ([self.commentModel.replyNumber integerValue] > 2) {
        return 40;
    }
    return 0.00001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = RGBOF(0xF6F6F6);
    [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if ([self.commentModel.replyNumber integerValue] > 2) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(15, 0, 120, 40);
        [btn setTitle:[NSString stringWithFormat:@"共%@条回复 >", self.commentModel.replyNumber] forState:UIControlStateNormal];
        [btn setTitleColor:RGBOF(0x4173AF) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(moreHuifu:) forControlEvents:UIControlEventTouchUpInside];
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [view addSubview:btn];
    }
    return view;
}

- (void)moreHuifu:(UIButton *)sender{
    CYCommentReplyListViewController *list = [[CYCommentReplyListViewController alloc] init];
    list.commentModel = self.commentModel;
    [[AppDelegate sharedAppDelegate] pushViewController:list];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
